package com.mofang.powerdekorhelper.activity.mine;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.persenter.InvalidataPersenter;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.GlideLoader;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.NetUtiles;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.ShareUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.InvalidataView;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2017/7/13 0013.
 */

public class MyInvalidataActivity extends MvpActivity<InvalidataView, InvalidataPersenter> implements InvalidataView {

    @BindView(R.id.invalidate_qrcode_imv)
    ImageView qrcodeImv;

    @BindView(R.id.invalidate_qrcode_title)
    TitleBar mTitleBar;

    @BindView(R.id.invalidate_qrcode_share)
    TextView qrcodeShareTv;

    private Dialog sharedDialog;

    private String qrCodeUrl;

    private File localQrcodeImage;

    private LoadPrograss loadPrograss;

    private List<Bitmap> qrCodeBitmaps;

    String id = "1";

    int type = 1;

    int userId;

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_invalidate_layout);
        initTitleBarWithback(mTitleBar, -1);
        mTitleBar.setDividerColor(getResources().getColor(R.color.white));
        loadPrograss = new LoadPrograss(this);
    }

    @Override
    public void initData() {

        userId = SharePerforenceUtils.getInstance(this).getUserID();

        presenter.getQrCode(type, userId, false);
    }

    @OnClick({R.id.invalidate_qrcode_share})
    void onclick(View v) {

        switch (v.getId()) {

            case R.id.invalidate_qrcode_share:

                final ShareUtils shareUtils = new ShareUtils(MyInvalidataActivity.this);

                sharedDialog = DialogUtils.getInstance().MySharedDialog(this, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {

                            case R.id.share_moments_imv:

                                showProgress();
                                String nodeId = SharePerforenceUtils.getInstance(MyInvalidataActivity.this).getNodeId();
                                String shareBgUrl = Constants.shareBgImageUrl1 + nodeId + "002.jpg";
                                new NetUtiles().getImageBitmap(MyInvalidataActivity.this, shareBgUrl, qrCodeUrl, new NetUtiles.ICorvertImage2() {
                                    @Override
                                    public void corvertSuccess(List<Bitmap> bitmap) {

                                        qrCodeBitmaps = bitmap;

                                        localQrcodeImage = new NetUtiles().mergeBitmap(qrCodeBitmaps.get(0), qrCodeBitmaps.get(1));

                                        new ShareUtils(MyInvalidataActivity.this).shareToTimeLine(MyInvalidataActivity.this, localQrcodeImage);

                                        //new ShareUtils(MyInvalidataActivity.this).shareLocalImage(MyInvalidataActivity.this, SHARE_MEDIA.WEIXIN_CIRCLE, localQrcodeImage, "");

                                        hideProgress();
                                    }

                                    @Override
                                    public void covertFail() {
                                        toast("二维码获取失败,请重试");
                                    }
                                });

                                break;
                            case R.id.share_wechat_imv:

                                String node3Img = SharePerforenceUtils.getInstance(MyInvalidataActivity.this).getNode3Image();

                                new AsyncTask<String, Void, Bitmap>() {

                                    @Override
                                    protected Bitmap doInBackground(String... params) {
                                        return new NetUtiles().getBitmap(params[0]);
                                    }

                                    @Override
                                    protected void onPostExecute(Bitmap bitmap) {
                                        super.onPostExecute(bitmap);
                                        if (bitmap != null) {
                                            shareUtils.shareMinProgram("圣象小程序", "圣象小程序", Constants.INVILITE_CODE, userId, bitmap);
                                        } else {
                                            toast("抱歉，获取分享数据失败");
                                        }

                                    }
                                }.execute(node3Img);
                                break;

                            case R.id.share_cancle_tv:
                                sharedDialog.dismiss();
                                break;
                        }
                        sharedDialog.dismiss();
                    }
                });
                sharedDialog.show();
                break;
        }
    }

    @Override
    public void setQrCode(String qrCodeUrl) {

        if (qrCodeUrl != null && !qrCodeUrl.equals("")) {
            this.qrCodeUrl = qrCodeUrl;
            GlideLoader.getInstance(this).loadImage(qrCodeUrl, qrcodeImv);
        }
        loadPrograss.hide();
    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
        toast(errorInfo);
    }

    @Override
    public InvalidataPersenter initPresenter() {
        return new InvalidataPersenter();
    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.stopRequest();
    }
}
